﻿using MediatR;
using SmartProduction.Domain;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.JobCommand_工作中心;

namespace SmartProduction.Write.API.Application.CommandHandlers.JobCommandHandler_工作中心
{
    public class JobDelCommandHandler:IRequestHandler<JobDelCommand,APIResult<object>>
    {
        private readonly IBaseRepository<Job> jobRepository;

        public JobDelCommandHandler(IBaseRepository<Job> jobRepository)
        {
            this.jobRepository = jobRepository;
        }

        public async Task<APIResult<object>> Handle(JobDelCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var entity=jobRepository.GetAll().Result.FirstOrDefault(x=>x.JId==request.JId);
            entity.IsDelete = 1;
            var i=jobRepository.UpdateAsync(entity).Result;
            if (i > 0)
            {
                result.Code = APIEnums.删除成功;
                result.Msg = APIEnums.删除成功.ToString();
                result.data = i;
            }
            else
            {
                result.Code = APIEnums.删除失败;
                result.Msg = APIEnums.删除失败.ToString();
                result.data = i;
            }
            return await Task.FromResult(result);
        }
    }
}
